package stackdemo;

import java.util.Stack;

/**
 * @Author 12629
 * @Date 2022/3/16 19:45
 * @Description：
 */
public class TestDemo {


    public static void main(String[] args) {
        MyStack myStack = new MyStack();
        myStack.push(1);
        myStack.push(2);
        myStack.push(3);
        myStack.push(4);
        System.out.println(myStack.pop());//4
        System.out.println(myStack.peek());//3
        System.out.println(myStack.pop());//3
        System.out.println(myStack.pop());//2
        System.out.println(myStack.pop());//1
        System.out.println(myStack.empty());


        System.out.println(myStack.pop());

    }


    public boolean IsPopOrder(int [] pushA,int [] popA) {
        Stack<Integer> stack = new Stack<>();
        int j = 0;//用来遍历popA数组
        for(int i = 0; i < pushA.length;i++) {
            stack.push(pushA[i]);
            //看啥时 可以出栈
            while(j < popA.length && !stack.empty() && stack.peek() == popA[j]) {
                stack.pop();
                j++;
            }
        }
        return stack.empty();
    }

    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for(int i = 0; i < s.length();i++) {
            char ch = s.charAt(i);
            if(ch == '(' || ch == '[' || ch == '{') {
                //说明此时是左括号
                stack.push(ch);
            }else{
                //说明是右括号
                //1、栈空不空？空不匹配-》右括号多，一上来就是右括号
                if(stack.empty()) {
                    return false;
                }
                char top = stack.peek();//一定是一个左括号
                if(ch == ')' && top == '(' || ch == '}' && top == '{' ||
                        ch == ']' && top == '[') {
                    stack.pop();
                }else{
                    return false;//左右括号不匹配的
                }
            }
        }
        if(!stack.empty()) {
            return false;//字符串都遍历完了，你的栈里面 还有括号。此时说明左括号多
        }
        return true;
    }

    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for(String x : tokens) {
            if(!isOperation(x)) {
                //说明当前的x是一个数字字符串
                stack.push(Integer.parseInt(x));
            }else{
                int num2 = stack.pop();
                int num1 = stack.pop();
                switch(x) {
                    case "+":
                        stack.push(num1+num2);
                        break;
                    case "-":
                        stack.push(num1-num2);
                        break;
                    case "*":
                        stack.push(num1*num2);
                        break;
                    case "/":
                        stack.push(num1/num2);
                        break;
                }
            }
        }
        return stack.pop();
    }

    //判断某个字符串是不是运算符
    private boolean isOperation(String s) {
        if(s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/")) {
            return true;
        }
        return false;
    }
    public static void main1(String[] args) {
        Stack<Integer> stack = new Stack<>();
        stack.push(12);
        stack.push(23);
        stack.push(34);
        stack.push(45);
        int ret = stack.peek();
        System.out.println(ret);
        System.out.println(stack.empty());
        System.out.println(stack.isEmpty());
    }
}
